package com.xb.xbpm.controller;

import com.xb.xbpm.service.ISiteService;
import com.xb.xbpm.utils.EsUtils;
import com.xb.xbpm.utils.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 广告位置分析
 * @Author gxd
 * @Date： 2018/8/09
 */
@Controller
@RequestMapping(value = "adv")
public class AdvPositionController {

    @Autowired
    EsUtils esUtils;
    @Autowired
    ISiteService siteService;


    @RequestMapping("/getAdvDatas")
    public @ResponseBody
    Map<String, Object> getAdvDatas(HttpServletRequest request){
        ModelMap map=new ModelMap();

        List<Map<String, Object>> topDatas=new ArrayList<Map<String,Object>>();
        List<Map<String, Object>> lowDatas=new ArrayList<Map<String,Object>>();

        List<String> datelist=null;
        String startDate="";
        String endDate="";
        String siteid= request.getParameter("siteid");
        String date= request.getParameter("date");
        String userid= request.getParameter("userid");

        if(Tools.isEmpty(siteid)){
            List<Map<String,Object>> siteList= new ArrayList<Map<String,Object>>();
            siteList=siteService.getSiteList(userid);
            if(siteList.size()>0){
                siteid=siteList.get(0).get("id").toString();
            }else{
                //直接返回数据 选择域名
                map.put("topDatas",topDatas);
                map.put("lowDatas",lowDatas);
                return map;
            }
        }
        if(Tools.notEmpty(date)){//日期不为空
            String [] strs=date.split(",");
            startDate=strs[0];
            endDate=strs[1];
        }else{//默认当天
            startDate=Tools.date2Str(new Date(),"yyyyMMdd");
        }
        try {
            //拼接索引
            String index="";
            if(Tools.isEmpty(endDate) || startDate.equals(endDate)){
                //一天的
                index="xblog_"+userid+"_"+startDate.replaceAll("-","");
            }else{
                //时间段内的所有索引值
                DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
                Date start=null;
                Date end=null;
                start = format1.parse(startDate);
                end= format1.parse(endDate);
                datelist= getBetweenDates(start,end);
                //datelist.add(startDate.replace("-",""));
                datelist.add(endDate.replace("-",""));

                for (String s: datelist ) {
                    index+="xblog_"+userid+"_"+s+",";
                }
            }

            //索引暂时写死
            //index="xblog_lm1_20180807";
            //siteid="32545";
            topDatas=esUtils.getAdvLoadTop(index,"doc",siteid,false);//最高
            lowDatas=esUtils.getAdvLoadTop(index,"doc",siteid,true);//最低

        }catch (Exception e){
            e.getStackTrace();
        }
        map.put("topDatas",topDatas);
        map.put("lowDatas",lowDatas);
        return map;
    }



    /**
     * 获取两个日期之间的日期
     * @param start 开始日期
     * @param end 结束日期
     * @return 日期集合
     */
    private static List<String> getBetweenDates(Date start, Date end) {
        DateFormat format1 = new SimpleDateFormat("yyyyMMdd");
        List<String> result = new ArrayList<String>();
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(start);
        //tempStart.add(Calendar.DAY_OF_YEAR, 1);

        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(end);
        while (tempStart.before(tempEnd)) {
            result.add(format1.format(tempStart.getTime()));
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        return result;
    }

}
